package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ConnectionHandler {
    Server chatServer;
    int port;

    public ConnectionHandler(int port, Server server) {
        this.port = port;
        chatServer = server;
    }

    void start() {
        System.out.println("starting server...");
        new Thread() {
            @Override
            public void run() {
                Client client;
                Socket socket;
                try (ServerSocket serverSocket = new ServerSocket(port)) {
                    while (true) {
                        try {
                            socket = serverSocket.accept();
                            client = new Client(socket, chatServer);
                            client.startCommunicate();
                        } catch (IOException e) {
                            System.out.println("unsuccessful connection");
                        }
                    }
                } catch (IOException e) {
                    System.out.println("error to start server");
                }
            }
        }.start();
    }

}
